package com.yunlankeji.stemcells.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.yunlankeji.ganxibaozhijia.databinding.AddConentBinding;
import com.yunlankeji.ganxibaozhijia.databinding.AddConentImgBinding;
import com.yunlankeji.stemcells.BaseApplication;
import com.yunlankeji.stemcells.entity.AddInformation;

import java.util.List;

public class AddAdapter extends BaseAdapter {

    private List<AddInformation> list;
    private DeleteOnClickListener delete;
    private PhotoOnClickListener photo;

    public AddAdapter(List<AddInformation> list,DeleteOnClickListener delete, PhotoOnClickListener photo) {
        this.list = list;
        this.delete = delete;
        this.photo = photo;
    }

    @Override
    public int getCount() {
        return list==null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AddConentImgBinding binding1;
        AddConentBinding binding2;
        AddInformation addInformation = list.get(position);
        if(addInformation.getType().equals("IMG")){
            binding1 = AddConentImgBinding.inflate(LayoutInflater.from(BaseApplication.getAppContext()), parent, false);
            binding1.imgDelete.setOnClickListener(v -> delete.deleteView(position));
            binding1.rlImg.setOnClickListener(v -> photo.photoView(position));
            convertView = binding1.getRoot();
            if(addInformation.getContent()!=null){
                Glide.with(BaseApplication.getAppContext())
                        .load(addInformation.getContent())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(binding1.imgPath);
            }
        }else {
            binding2 = AddConentBinding.inflate(LayoutInflater.from(BaseApplication.getAppContext()), parent, false);
            binding2.tvTitleContent.setText(addInformation.getContent());
            binding2.imgDelete.setOnClickListener(v -> {
                delete.deleteView(position);
            });
            convertView = binding2.getRoot();
            binding2.tvTitleContent.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    list.get(position).setContent(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }

        return convertView;
    }

    /**
     * 定义一个移除接口
     */
    public interface DeleteOnClickListener{
        void deleteView(int position);
    }
    /**
     *定义图片加载接口
     */
    public interface PhotoOnClickListener{
        void photoView(int position);
    }
}
